package com.sysu.minsysu.wdzj;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;
import com.minSYSU.db.DatabaseHelper;
import com.minSYSU.db.LocationInfo;
import com.minSYSU.db.BuildingInfo;
import com.sysu.minsysu.R;


public class ShowNewMessage extends MapActivity {
	 /** Called when the activity is first created. */
	private double lat = 23.066752;
	private double lon = 113.391974;
	
	
	MapView mapView;
	private MapController mc;
	private GeoPoint point;
	View popView;
	private ListView list;
	public ArrayList<BuildingInfo> placeInfos;
	

   @Override
   public void onCreate(Bundle savedInstanceState) {
   	super.onCreate(savedInstanceState);
       setContentView(R.layout.show_new_message);
       mapView = (MapView) findViewById(R.id.mapView);
       mapView.setBuiltInZoomControls(true);
       
       mc = mapView.getController();
       point = new GeoPoint((int)(lat*1E6),(int)(lon*1E6));
       mc.setCenter(point);
       mc.setZoom(20);
       
       placeInfos = LocationInfo.getEastMapInfo();
       
       //画overlay
       popView = super.getLayoutInflater().inflate(R.layout.popview, null);
       mapView.addView( popView,new MapView.LayoutParams(	
       									MapView.LayoutParams.WRAP_CONTENT, 
       									MapView.LayoutParams.WRAP_CONTENT,
               							null, MapView.LayoutParams.BOTTOM_CENTER));
       popView.setVisibility(View.GONE);
       
       //画点
       Drawable marker = getResources().getDrawable(R.drawable.poi_1);
		marker.setBounds(0, 0, 20, 20);
		mapView.getOverlays().add(new OverItemT(marker, this, point));	
		
		//加载抽屉栏数据
		setListContent();

   }
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub 
		return false;
	}
   
	//抽屉
	private void setListContent() {
		// TODO Auto-generated method stub
		list = (ListView) findViewById(R.id.list);

		//生成动态数组，并且转载数据  
		ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();  
		for(int i=0;i<30;i++)  
		{  
		    HashMap<String, String> map = new HashMap<String, String>();  
		    map.put("ItemTitle", "test");  
		    mylist.add(map);  
		}  
		SimpleAdapter mSchedule = new SimpleAdapter(this,
		                                            mylist,//数据来源   
		                                            R.layout.list_item,//ListItem的XML实现                                                                                                                 
		                                            new String[] {"ItemTitle"},//动态数组与ListItem对应的子项    
		                                            new int[] {R.id.text});//ListItem的XML文件里面的两个TextView ID    
		//添加并且显示  
		list.setAdapter(mSchedule); 
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		MyMessage.db.close();
		super.onDestroy();
	}
}



class OverItemT extends ItemizedOverlay<OverlayItem>{

	private Drawable marker;
	private ShowNewMessage mContext;
	private List<OverlayItem> GeoList = new ArrayList<OverlayItem>();
	GeoPoint mpoint;
	
	public OverItemT(Drawable defaultMarker, ShowNewMessage context, GeoPoint point) {
		//super(boundCenterBottom(defaultMarker));
		super(defaultMarker);
		// TODO Auto-generated constructor stub
		double lat = 23.066752;
		double lon = 113.392474;
	
		this.marker = defaultMarker;
		this.mContext = context;
		this.mpoint = point;
		
		MyMessage.db = new DatabaseHelper(mContext);      
		Log.e("timerange", MinSYSUMessageActivity.timeRange[0] + " " + MinSYSUMessageActivity.timeRange[0]);
		if (MinSYSUMessageActivity.timeRange[0] == null && MinSYSUMessageActivity.timeRange[1] == null) {
			Log.e("db all", String.valueOf(MyMessage.myCursor.getCount()));
			MyMessage.myCursor = MyMessage.db.getAllRecords();
		} else {
			Log.e("db timerange", String.valueOf(MyMessage.myCursor.getCount()));
			MyMessage.myCursor = MyMessage.db.getRecordsByTime(MinSYSUMessageActivity.timeRange[0], MinSYSUMessageActivity.timeRange[1]);
		}
		MyMessage.myCursor.moveToFirst();
		
		for (int i = 0; i < MyMessage.myCursor.getCount(); i++) {
//			Log.e("------", String.valueOf(MyMessage.myCursor.getCount()));
//			Log.e("+++++++++", String.valueOf(MyMessage.myCursor.getColumnIndexOrThrow("Location")));
			String place = MyMessage.myCursor.getString(MyMessage.myCursor.getColumnIndexOrThrow("Location"));
			for (int j = 0; j < mContext.placeInfos.size(); j++) {
				if (mContext.placeInfos.get(j).getName().equals(place)) {
					lat = mContext.placeInfos.get(j).getLatitude();
					lon = mContext.placeInfos.get(j).getLongitude();
					GeoPoint p = new GeoPoint((int)(lat*1E6),(int)(lon*1E6));
					//添加地图上显示的点
					GeoList.add(new OverlayItem(p, 
							MyMessage.myCursor.getString(MyMessage.myCursor.getColumnIndexOrThrow("userID")), 
							MyMessage.myCursor.getString(MyMessage.myCursor.getColumnIndexOrThrow("Content"))));
				}
			}
			MyMessage.myCursor.moveToNext();
		}
		MyMessage.myCursor.close();
		populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		// TODO Auto-generated method stub
		return GeoList.get(i);
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return GeoList.size();
	}
	
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		// TODO Auto-generated method stub
		
		Projection projection = mapView.getProjection(); 
		for (int index = 0; index < size(); index++) {
//			Log.e("overlay draw()", String.valueOf(index));
			OverlayItem overLayItem = getItem(index); 
			
			String title = overLayItem.getTitle();
			Point point = projection.toPixels(overLayItem.getPoint(), null); 
			
			Paint paintCircle = new Paint();
			paintCircle.setColor(Color.RED);

			canvas.drawCircle(point.x, point.y, 3, paintCircle); // 画圆
//			Log.e("overlay draw()", String.valueOf(point.x) + "," + String.valueOf(point.y));
			
//			Paint paintText = new Paint();
//			paintText.setColor(Color.BLACK);
//			paintText.setTextSize(15);		
//			canvas.drawText(title, point.x, point.y - 25, paintText); // 绘制文本

		}
		
		super.draw(canvas, mapView, shadow);
	}
	
	@Override
	protected boolean onTap(int i) {
		// TODO Auto-generated method stub
		setFocus(GeoList.get(i));
		MapView.LayoutParams geoLP = (MapView.LayoutParams) mContext.popView.getLayoutParams();
		geoLP.point = GeoList.get(i).getPoint();
		mContext.mapView.updateViewLayout(mContext.popView, geoLP);
		mContext.popView.setVisibility(View.VISIBLE);
		
		//设置点击出现内容
		TextView author = (TextView) mContext.findViewById(R.id.author);
		TextView tv = (TextView) mContext.findViewById(R.id.tv);
//		if(i == 0){
//			/*
//			 * to do
//			 * 读取数据库，显示发表者和内容
//			 */
//			author.setText("MM:");
//			tv.setText("郁闷呀！");
//		} else if (i == 1){
//			author.setText("GG:");
//			tv.setText("纠结呀！");
//		} else if (i == 1){
//			author.setText("DD:");
//			tv.setText("纠结呀3！");
//		} else{
//			author.setText("JJ:");
//			tv.setText("纠结呀4！");
//		}
		author.setText(GeoList.get(i).getTitle());
		tv.setText(GeoList.get(i).getSnippet());
		
		return true;
	}
}
